<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$isarea=intval($_RQ['isarea']);
	$isxuni=intval($_RQ['isxuni']);
	$return['setting']=$this->plugin_setting('page_setting');
	$params=array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'type'=>array(0,1,3,4));
	$return['commission_money']=$this->sum('commission',$params,'money');
	$return['commission_jifen']=$this->sum('commission',$params,'jifen');
	if(!$return['commission_money']){
		$return['commission_money']=0;
	}
	$return['commission_total']=$this->count('commission',$params);
	$params['type']=20;
	unset($params['level']);
	$return['jichanum']=$this->count('commission',$params);
	$return['list']=array();
	$return['params']=array(
		'isarea'=>$isarea,
		'isxuni'=>$isxuni,
		'level'=>1,
		'page'=>1,
	);
	$return['psize']=20;
	set_html('title','佣金明细');
	$return['xuniname']=$this->xuniname;
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'type'=>array(0,1,3,4));
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('commission',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$order=$this->get('agent_order',array('ordersn'=>$item['ordersn']));
		$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn']),'id');
		$item['member']=model('core/member')->basic($order['mid']);
		$item['statustitle']=$this->commission_status[$item['status']];	
		$item['typetitle']=$this->commission_money_types[$item['type']];
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$merchs =array();
		$item['feemoney']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
			$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			if($feemoneyset['isopen']==1){
				$gv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$item['price']:$feemoneyset['feerade']*$item['price']/100;
				$gv['feemoney']=$gv['total']*$gv['feemoney'];
				$item['feemoney'] +=$gv['feemoney'];
				$goodsinfo['feemoney']=price($gv['feemoney']);
			}
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
		}
		$item['merchs']=$merchs;
	}
	exi($list);
}
if($_RQ['op']=='xunibalance'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>1);
	$money=pdo_getsum('jy_mjicai_commission',$params,'money');
	cfc('core')->member_credit($_SESSION['uid'],'credit'.$this->setting['credit_type'],$money,true,'jy_mjicai','分销奖励结算');
	$update=array(
		'status'=>2,
	);
	$this->update('commission',$update,$params);
	exi('结算成功！','','/index/');
}
?>